<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI 百科全书</title>
   <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: #f0f2f5;
            margin: 0;
            color: #333;
        }
        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
            box-sizing: border-box;
            text-align: center;
        }
        h1 {
            color: #007bff;
            margin-bottom: 25px;
            font-size: 2em;
        }
        .input-area {
            display: flex;
            margin-bottom: 20px;
            gap: 10px;
        }
        input[type="text"] {
            flex-grow: 1;
            padding: 12px 15px;
            border: 1px solid #ced4da;
            border-radius: 8px;
            font-size: 1em;
            outline: none;
            transition: border-color 0.3s;
        }
        input[type="text"]:focus {
            border-color: #007bff;
            box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
        }
        button {
            padding: 12px 25px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 1em;
            transition: background-color 0.3s ease, transform 0.1s ease;
            box-shadow: 0 4px 10px rgba(0, 123, 255, 0.2);
        }
        button:hover {
            background-color: #0056b3;
            transform: translateY(-1px);
        }
        button:active {
            transform: translateY(0);
            box-shadow: 0 2px 5px rgba(0, 123, 255, 0.3);
        }
        .response-area {
            background-color: #e9ecef;
            padding: 20px;
            border-radius: 8px;
            text-align: left;
            min-height: 100px;
            max-height: 400px;
            overflow-y: auto;
            border: 1px solid #dee2e6;
            white-space: pre-wrap; /* 保留空白符和换行 */
            word-wrap: break-word; /* 自动换行 */
        }
        .response-area p {
            margin: 0;
            line-height: 1.6;
            color: #495057;
        }
        .loading-indicator {
            margin-top: 15px;
            font-style: italic;
            color: #6c757d;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI 在线问答</h1>
        <div class="input-area">
            <input type="text" id="questionInput" placeholder="问我任何问题..." autofocus>
            <button id="askButton">提问</button>
        </div>
        <div class="response-area" id="responseArea">
            <p>AI 的回答会显示在这里。</p>
        </div>
        <div class="loading-indicator" id="loadingIndicator" style="display: none;">
            AI 正在思考中...
        </div>
    </div>

    <script>
        //获取DOM元素
        const questionInput = document.getElementById('questionInput');
        const askButton = document.getElementById('askButton');
        const responseArea = document.getElementById('responseArea');
        const loadingIndicator = document.getElementById('loadingIndicator');
    
        //监听按钮点击事件
        askButton.addEventListener('click', async () => {
            const question = questionInput.value.trim();
            if (!question) {
                alert('请输入你的问题！');
                return;
            }
    
            responseArea.innerHTML = ''; // 清空之前的回答
            loadingIndicator.style.display = 'block'; // 显示加载提示
            askButton.disabled = true; // 禁用按钮，防止重复点击
            questionInput.disabled = true; // 禁用输入框
    
            try {//获取后端数据
                const response = await fetch('http://127.0.0.1:3000/api/ask', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        message: question,
                        historyMsg: []
                    })
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const data = await response.json();
                responseArea.innerHTML = `<p>${data.response}</p>`;
            } catch (error) {
                console.error('Error:', error);
                responseArea.innerHTML = `<p style="color: red;">出错了: ${error.message}</p>`;
            } finally {
                loadingIndicator.style.display = 'none'; // 隐藏加载提示
                askButton.disabled = false; // 启用按钮
                questionInput.disabled = false; // 启用输入框
                questionInput.value = ''; // 清空输入框
            }
        });
    
        // 允许按回车键提问
        questionInput.addEventListener('keypress', (event) => {
            if (event.key === 'Enter') {
                askButton.click();
            }
        });
    </script>
</body>
</html>